import { OPERATION_TYPE, PAGE_STATUS } from "@/const/widget";
const state = {
  isPageModified: false, //是否被修改过
  page: {}, //页面数据
  activeComponentJSON: {}, //新增/移动时的活动json
  addActiveComponentIndex: -1, //新增时候的组件index
  editConfigComActiveId: 0, //在结束左侧拖拽时，在预览上下拖拽时，在点击的时候，可编辑属性的组件id
  operationStatus: OPERATION_TYPE.ADD, //页面操作状态
  status: PAGE_STATUS.OFF, //页面状态
  isPreview: false, //是否预览
  cusFormEmptyTipIds: [],
  shotCover: "", //截图
};

const mutations = {
  SET_PAGE_JSON: (state, data) => {
    state.page = data;
  },
  SET_OP_STATUS: (state, data) => {
    state.operationStatus = data;
  },
  SET_PAGE_MODIFIED_STATUS: (state, data) => {
    state.isPageModified = data;
  },
  SET_CUSFORM_EMPTY_TIP_IDS: (state, data) => {
    state.cusFormEmptyTipIds = data;
  },
  SET_EDIT_CONFIG_COM_ACTIVE_ID: (state, data) => {
    state.editConfigComActiveId = data;
  },
  SET_PAGE_COMPONENTS: (state, data) => {
    const { index, json, count = 0 } = data;
    count
      ? state.page.items.splice(index, count)
      : state.page.items.splice(index, count, json);
  },
  SET_PAGE_COMPONENT_JSON: (state, data) => {
    state.activeComponentJSON = data;
  },
  SET_ADD_PAGE_COMPONENT_INDEX: (state, data) => {
    state.addActiveComponentIndex = data;
  },
  DEL_COMPONENT: (state, data) => {
    state.page.items.splice(data, 1);
  },
  SET_PAGE_STATUS: (state, data) => {
    state.status = data;
  },

  SET_PAGE_PrREVIEW: (state, data) => {
    state.isPreview = data;
  },
  SET_SHOT_COVER: (state, data) => {
    state.shotCover = data;
  },
};

const actions = {
  setPageJson({ commit }, data) {
    commit("SET_PAGE_JSON", data);
  },
  setOperationStatus({ commit }, data) {
    commit("SET_OP_STATUS", data);
  },
  changePageModifiedStatus({ commit }, data) {
    commit("SET_PAGE_MODIFIED_STATUS", data);
  },
  setCusFormEmptyTipIds({ commit }, data) {
    commit("SET_CUSFORM_EMPTY_TIP_IDS", data);
  },
  setShotCover({ commit }, data) {
    commit("SET_SHOT_COVER", data);
  },
  setMovePageComponents({ commit }, data) {
    console.log("setMovePageComponents", data);
    const { newIndex, oldIndex, json } = data;
    commit("SET_PAGE_COMPONENTS", {
      json,
      index: oldIndex,
      count: 1,
    });
    commit("SET_PAGE_COMPONENTS", {
      json,
      index: newIndex,
      count: 0,
    });
  },
  setPageComponentJSON({ commit }, data) {
    commit("SET_PAGE_COMPONENT_JSON", data);
  },
  setAddPageComponentIndex({ commit }, data) {
    commit("SET_ADD_PAGE_COMPONENT_INDEX", data);
  },
  setEditConfigComActiveId({ commit }, data) {
    commit("SET_EDIT_CONFIG_COM_ACTIVE_ID", data);
  },
  setAddPageComponent({ commit, state }) {
    commit("SET_PAGE_COMPONENTS", {
      json: state.activeComponentJSON.json,
      index: state.addActiveComponentIndex,
      count: 0,
    });
  },
  delComponent({ commit }, data) {
    commit("DEL_COMPONENT", data);
  },
  setPageStatus({ commit }, data) {
    commit("SET_PAGE_STATUS", data);
  },
  setPagePreview({ commit }, data) {
    commit("SET_PAGE_PrREVIEW", data);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
